package com.powernode.settings.service.impl;

import cn.hutool.core.util.StrUtil;
import com.powernode.commons.utils.ResponseData;
import com.powernode.commons.utils.UUIDUtils;
import com.powernode.settings.bean.Permission;
import com.powernode.settings.bean.UserBean;
import com.powernode.settings.dao.PermissionMapper;
import com.powernode.settings.dao.UserMapper;
import com.powernode.settings.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper dao;

    /**
     * 查询所有的权限   树形数据结构
     * @return
     */
    @Override
    public ResponseData findAll() {
        return ResponseData.ok(dao.selectAll());
    }

    /**
     * 根据角色id查询权限
     * @param roleId
     * @return
     */
    @Override
    public ResponseData findPermissionIdByRoleId(String roleId) {
        return ResponseData.ok(dao.selectPermissionIdByRoleId(roleId));
    }

    /**
     * 给角色重新分配权限
     * 1. 删除角色之前的权限
     * 2. 重新添加角色的权限
     * @param roleId
     * @param pids
     * @return
     */
    @Override
    @Transactional
    public ResponseData modifyRolePermission(String roleId, String pids) {
        dao.deleteRolePermission(roleId);
        if(!StrUtil.isEmpty(pids)){
            dao.insertRolePermission(roleId,pids.split(","));
        }

        return ResponseData.ok();
    }

    /**
     * 添加菜单
     * @param permission
     * @return
     */
    @Override
    public ResponseData add(Permission permission) {
        //设置主键id
        permission.setId(UUIDUtils.getUUID());
        int row = this.dao.insert(permission);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 修改
     * @param p
     * @return
     */
    @Override
    public ResponseData edit(Permission p) {
        int row = dao.updateByPrimaryKeySelective(p);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     *
     * 批量删除菜单权限
     * 注意: 先查看role_permission 看权限id是否在里面
     * @param ids
     * @return
     */
    @Override
    public ResponseData removeBatch(String ids) {
        List<String> list = dao.selectRolePermissionByPermissionIds(ids.split(","));
        //权限正在被使用
        if (list.size()>0){
            return ResponseData.error("权限正在被使用,禁止删除");
        }
        int row = dao.deleteBatch(ids.split(","));
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

@Autowired
    private UserMapper userMapper;
    /**
     * 根据用户的id查询权限
     * @param userId
     * @return
     */
    @Override
    public ResponseData findPermissionsByUserId(String userId) {
        UserBean user = userMapper.selectByPrimaryKey(userId);
        //超级管理员拥有所有的权限
        if (user.getAdmin()==1){
            //树形菜单也有了
            return  ResponseData.ok(dao.selectAllPermissions());
        }
        return ResponseData.ok(dao.selectPermissionByUserId(userId));
    }
}
